<?php

namespace app\admin\controller;

use app\common\business\AdminPermission;
use app\common\business\AdminUser;
use app\common\lib\Param;
use app\common\lib\Show;
use think\response\Json;

class User extends AdminBase
{
    /**
     * 查询当前登录用户信息及角色
     * @return Json
     */
    public function getUserInfo(): Json
    {
        try {
            $user=(new AdminUser())->getAdminUserById($this->adminId);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        return Show::success($user);
    }

    /**
     * 获取当前登录用户对应权限的菜单
     * @return Json
     */
    public function getMenuList(): Json
    {
        try {
            $menu=(new AdminPermission())->getPermission($this->adminId);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        return Show::success($menu);
    }

    /**
     * 管理后台列表
     * @return Json
     */
    public function list(): Json
    {
        list($keyword, $page,$limit) = Param::getMore([
            ['keyword', ''],
            ['page', null],
            ['limit', null],
        ], $this->request, true);
        try {
            $list=(new AdminUser())->getTableList($page,$limit,$keyword);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        return Show::success($list);
    }
    /**
     * 新增/保存数据
     * @return Json
     */
    public function save(): Json
    {
        list($id, $status,$roles,$password,$realName,$username,$phone) = Param::postMore([
            ['id',null],
            ['status', 1],
            ['roles', 0],
            ['password', ''],
            ['real_name', ''],
            ['username', ''],
            ['phone', ''],
        ], $this->request, true);

        $data=[
            'id'=>$id,
            'status'=>$status,
            'roles'=>$roles,
            'password'=>$password,
            'real_name'=>$realName,
            'username'=>$username,
            'phone'=>$phone,
        ];
        $validate = new \app\admin\validate\User();
        $scene=empty($data['id'])?'create':'update';
        if (!$validate->scene($scene)->check($data)) {
            return Show::error([], $validate->getError());
        }
        try {
            $result = (new AdminUser())->save($data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "保存成功");
        }
        return Show::error([], '保存失败');
    }
    /**
     * 修改密码
     * @return Json
     */
    public function changePwd(): Json
    {
        list($password) = Param::postMore([
            ['password', ''],
        ], $this->request, true);

        $data=[
            'id'=>$this->adminId,
            'password'=>$password,
        ];
        $validate = new \app\admin\validate\User();
        if (!$validate->scene('changePwd')->check($data)) {
            return Show::error([], $validate->getError());
        }
        try {
            $result = (new AdminUser())->save($data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "保存成功");
        }
        return Show::error([], '保存失败');
    }
    /**
     * 修改状态
     * @return Json
     */
    public function updateStatus(): Json
    {
        list($id, $status) = Param::getMore([
            ['id',null],
            ['status', null],
        ], $this->request, true);

        $data=[
            'id'=>$id,
            'status'=>$status,
        ];
        try {
            $result = (new AdminUser())->updateStatus($data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "保存成功");
        }
        return Show::error([], '保存失败');
    }
    /**
     * 根据id删除
     * @return Json
     */
    public function deleteById(): Json
    {
        $id=$this->request->get('id');
        try {
            $result=(new AdminUser())->deleteById($id);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "删除成功");
        }
        return Show::error([], '删除失败');
    }
}
